﻿/*------------------------------------------------------------
 * Author : Tharinduni Udugamasuriya
 * FinCon dll for Handeling DB 
 * RegistryManager : Registry Read/ Write Management - outer
 -----------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSPL.Contracts.Interfaces;


namespace DSPL.Contracts
{
    public class RegistryManager : IDisposable
    {
        #region Construtor

        public RegistryManager()
        { }

        #endregion
             
        #region IDisposable Members

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        ~RegistryManager() { }

        #endregion

        #region Decalare Private Variables

        private string _finConStr;
        private string _finServer;
        private string _finUser;
        private string _finPassword;
        private string _finDb;
        //private SqlConnection _finDBCon;

        private List<IParameter> regVal = new List<IParameter>();

        #endregion

        #region Decalare & Assign Public Variables

        public string FinConStr
        {
            get
            {
                _finConStr = ReadKey("FinConStr");
                return _finConStr;
            }
            set { _finConStr = value; }
        }

        public string FinServer
        {
            get
            {
                _finServer = ReadKey("FinServer");
                return _finServer;
            }
            set { _finServer = value; }
        }

        public string FinUser
        {
            get
            {
                _finUser = ReadKey("FinUser");
                return _finUser;
            }
            set { _finUser = value; }
        }

        public string FinPassword
        {
            get
            {
                _finPassword = ReadKey("FinPassword");
                return _finPassword;
            }
            set { _finPassword = value; }
        }

        public string FinDb
        {
            get
            {
                _finDb = ReadKey("FinDb");
                return _finDb;
            }
            set { _finDb = value; }
        }

        #endregion

        #region Define Class Objects
        Cipher objCipher = new Cipher();
        RegistryController objRegMgr = new RegistryController();
        #endregion

        #region Public Methods

        public bool SaveDbConnection(string server = null, string username = null, string password = null, string database = null, string constring = null)
        {
            //try
            //{
            //    if (!constring.Equals(null))
            //    {
            //        _finConStr = constring;
            //        //regVal.Add(new Parameter("FinConStr", objCipher.Encrypt(_finConStr, true)));
            //        regVal.Add(new IParameter{ ParamName = "FinConStr", ParamValue = _finConStr });
            //    }
            //    else
            //    {
            //        return false;
            //    }
            //    if (!server.Equals(null))
            //    {
            //        _finServer = server;
            //        //regVal.Add(new Parameter("FinServer", objCipher.Encrypt(_finServer, true)));
            //        regVal.Add(new IParameter("FinServer", _finServer));
            //    }
            //    else
            //    {
            //        return false;
            //    }
            //    if (!username.Equals(null))
            //    {
            //        _finUser = username;
            //        //regVal.Add(new Parameter("FinUser", objCipher.Encrypt(_finUser, true)));
            //        regVal.Add(new IParameter("FinUser", _finUser));
            //    }
            //    else
            //    {
            //        return false;
            //    }
            //    if (!password.Equals(null))
            //    {
            //        _finPassword = password;
            //        //regVal.Add(new Parameter("FinPassword", objCipher.Encrypt(_finPassword, true)));
            //        regVal.Add(new IParameter("FinPassword", _finPassword));
            //    }
            //    else
            //    {
            //        return false;
            //    }
            //    if (!database.Equals(null))
            //    {
            //        _finDb = database;
            //        //regVal.Add(new Parameter("FinDb", objCipher.Encrypt(_finDb, true)));
            //        regVal.Add(new IParameter("FinDb", _finDb));
            //    }
            //    else
            //    {
            //        return false;
            //    }

            //    foreach (var item in regVal)
            //    {
            //        bool isWrite;
            //        isWrite = objRegMgr.Write(item.ParamName, item.ParamValue);
            //        if (!isWrite)
            //        {
            //            return false;
            //        }
            //    }
            //    return true;
            //}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
            //catch (Exception)
            //{
            //    return false;
            //}
            return true;
        }


        public string ReadKey(string keyword)
        {
            try
            {
                string keyVal = "";
                if (!keyword.Equals(null))
                {
                    keyVal = objRegMgr.Read(keyword);
                    //keyVal = objCipher.Decrypt(keyVal, true);
                }
                return keyVal;
            }
            catch (Exception)
            {
                return "";
            }
        }


        #endregion
    }
}
